package com.tuanzi.loan.business.service.family.impl;

import com.google.common.base.Joiner;
import com.tuanzi.loan.business.entity.family.CommSysCode;
import com.tuanzi.loan.business.entity.family.LoanAdvert;
import com.tuanzi.loan.business.repository.family.CommSysCodeRepository;
import com.tuanzi.loan.business.repository.family.LoanAdvertRepository;
import com.tuanzi.loan.business.service.family.LoanAdvertService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.service.CommonServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.family.LoanAdvertSearchRequest;
import com.tuanzi.loan.vo.response.family.LoanAdvertSearchResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author weibifeng 2017/09/21
 */
@Debug
@Service
@Slf4j
@Transactional
@Retryable(value = { RetryException.class })
public class LoanAdvertServiceImpl extends CommonServiceImpl<LoanAdvert, Long> implements LoanAdvertService {

	@Autowired
	private LoanAdvertRepository loanAdvertRepository;

	@Autowired
	private CommSysCodeRepository commSysCodeRepository;

	@Override
	protected JpaRepository<LoanAdvert, Long> getRepository() {
		return loanAdvertRepository;
	}

	public Integer offline(Long activityId) {
		return loanAdvertRepository.offlineByActivityId(activityId);
	}

	@Override
	public Page<LoanAdvertSearchResponse> list(PageSearcher<LoanAdvertSearchRequest> pageSearcher) {
		return loanAdvertRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
	}

	@Override
	public void logicDelete(Long id) {
		LoanAdvert loanAdvert = loanAdvertRepository.findOne(id);
		if(Objects.nonNull(loanAdvert)){
			loanAdvertRepository.delete(loanAdvert.getId());
		}
	}

	@Override
	public void changeStatus(Integer id) {
		loanAdvertRepository.changeStatus(id);
	}

	@Override
	public List<CommSysCode> findAdvert() {
		List<CommSysCode> commSysCodes = new ArrayList<CommSysCode>();
		commSysCodeRepository.findAll().forEach(a -> {
			if (a.getType().equals("advertise")) {
				commSysCodes.add(a);
			}
		});
		return commSysCodes;

	}

	@Override
	public void updatePosition(Integer position, Integer id) {
		loanAdvertRepository.updatePosition(position, id);
	}

	@Override
	public void batchEliminateProduct(List<Long> ids, List<String> excludeProductIds) {

		String excludeProductId = Joiner.on(",").join(excludeProductIds);

		for(Long id: ids){
			LoanAdvert loanAdvert = loanAdvertRepository.findOne(id);
			loanAdvert.setExcludePrdId(excludeProductId);

			loanAdvertRepository.save(loanAdvert);

		}

	}

	@Override
	public List<LoanAdvert> findByAdvertSpaceAndActivityId(Integer advertSpace, Long activityId) {
		return loanAdvertRepository.findByAdvertSpaceAndActivityId(advertSpace, activityId);
	}

}


























